package com.zhiyou100.action01;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yang
 * @className Demo04
 * @packageName com.zhiyou100.action01
 * @Description TODO 请求方式
 * @date 2020-08-21 16:25
 */


@Controller
@RequestMapping("/d4")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Demo04 {

    static {
        // 默认情况下， action 类是单例，并且是懒加载
        // @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 设置 为多例
        System.out.println("静态代码块");
    }

    {
        // 创建对象时加载
        System.out.println("构造代码块");
    }

    /**
     * 默认情况下， action 既能接受 get 也能接受 post 请求
     *
     * @param requestMap HttpServletRequest
     * @return String
     */
    @RequestMapping("/m1.action")
    public String method01(HttpServletRequest requestMap) {
        requestMap.setAttribute("message", "请求方式是 " + requestMap.getMethod());
        return "test01/success";
    }

    /**
     * method = {RequestMethod.GET} 指定只接受 get 请求
     *
     * @param requestMap HttpServletRequest
     * @return String
     */
    @RequestMapping(value = "/m2.action", method = {RequestMethod.GET})
    public String method02(HttpServletRequest requestMap) {
        requestMap.setAttribute("message", "请求方式是 " + requestMap.getMethod());
        return "test01/success";
    }

    /**
     * method = {RequestMethod.GET, RequestMethod.POST} 指定只接受 get 和 post 请求
     *
     * @param requestMap HttpServletRequest
     * @return String
     */
    @RequestMapping(value = "/m3.action", method = {RequestMethod.GET, RequestMethod.POST})
    public String method03(HttpServletRequest requestMap) {
        requestMap.setAttribute("message", "请求方式是 " + requestMap.getMethod());
        return "test01/success";
    }
}
